package com.lihua.newideas.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.lihua.newideas.pojo.Article;

import com.lihua.newideas.pojo.MyUser;
import com.lihua.newideas.service.MyUserService;
import com.lihua.newideas.service.impl.ArticleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//return "redirect:/admin"; 重定向
//文章控制器
@Controller
@RequestMapping("/admin")
public class ArticleController {

    @Autowired
    ArticleServiceImpl articleService;

    @Autowired
    MyUserService myUserService;

    // 去添加文章的视图
    @RequestMapping("/newArticle")
    public String newArticle() {
        return "admin/add_article";
    }


    //获取所有文章
    @RequestMapping("/allArticle")
    public String getArticleAll(Model model,
                           @RequestParam(value = "page", defaultValue = "1") int page,
                           @RequestParam(value = "limit", defaultValue = "8") int limit) {
        PageInfo<Article> articlePageInfo = articleService.queryArticleAll(page, limit);
//        System.out.println(articlePageInfo.toString());
        model.addAttribute("articlePageInfo", articlePageInfo);

        return "admin/article_all";
    }
    //获取草稿箱的文章
    @RequestMapping("/allArticleRelease")
    public  String getArticleAllRelease(Model model,
                                      @RequestParam(value = "page", defaultValue = "1") int page,
                                      @RequestParam(value = "limit", defaultValue = "8") int limit) {
        PageInfo<Article> articlePageInfo = articleService.queryArticleByArticleStatus(0,page, limit);
//        System.out.println(articlePageInfo.toString());
        model.addAttribute("articlePageInfo", articlePageInfo);

        return "admin/article_release";
    }
    //获取草稿箱的文章
    @RequestMapping("/allArticleDraft")
    public  String getArticleAllDraft(Model model,
                                      @RequestParam(value = "page", defaultValue = "1") int page,
                                      @RequestParam(value = "limit", defaultValue = "8") int limit) {
        PageInfo<Article> articlePageInfo = articleService.queryArticleByArticleStatus(1,page, limit);
//        System.out.println(articlePageInfo.toString());
        model.addAttribute("articlePageInfo", articlePageInfo);

        return "admin/article_draft";
    }

    //添加文章
    @RequestMapping("/addArticle")
    public @ResponseBody Article addArticle(Article article, HttpServletRequest request) {
        article.setArticleNewstime(DateUtil.date());
        article.setArticleUpdatetime(DateUtil.date());
        User user = (User) request.getSession().getAttribute("user");
        if (user!=null){
//            System.out.println(user.getUsername());
            MyUser myUser = myUserService.queryUserByName(user.getUsername());
//            System.out.println(myUser);
            article.setUserId(myUser.getId());
//            System.out.println(article);
            articleService.addArticle(article);
            
        }

        return article;
    }

    /*
    * 查看文章
    * */
    @RequestMapping("/articleDetails/{id}")
    public String articleDetailsMin(@PathVariable String id, Model model){
//        System.out.println(id);
        long id1 = Long.valueOf(id);
        Article article = articleService.queryArticleById(id1);
        model.addAttribute("article",article);
        return "article_details_min";
    }

    /*根据id删除文章
    * */
    @ResponseBody
    @RequestMapping("/deleteArticle/{id}")
    public String deleteArticle(@PathVariable String id){
        long id1 = Long.valueOf(id);

        return articleService.deleteArticleById(id1);
    }

    /*
    * 修改文章视图
    * */
    // 去添加文章的视图
    @RequestMapping("/editArticle/{id}")
    public String updateArticleView(@PathVariable("id") String id,Model model) {
        long id1 = Long.valueOf(id);
        Article article = articleService.queryArticleById(id1);
        model.addAttribute("article",article);
        return "admin/update_article";
    }
    /*根据id修改文章
    * */
    @PostMapping("/updateArticle")
    public @ResponseBody Article updateArticle(Article article, HttpServletRequest request){
//        article.setArticleNewstime(DateUtil.date());
        article.setArticleUpdatetime(DateUtil.date());
        User user = (User) request.getSession().getAttribute("user");
        if (user!=null){
//            System.out.println(user.getUsername());
            MyUser myUser = myUserService.queryUserByName(user.getUsername());
//            System.out.println(myUser);
            article.setUserId(myUser.getId());
//            System.out.println(article.toString());
            articleService.updateArticleById(article);
        }
        return article;
    }


}
